﻿using CustomLogicInsteadOfDelete.Module.BusinessObjects;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomLogicInsteadOfDelete.Module.Controllers {
  public class TStudentVc : ViewController<ListView> {
    public TStudentVc() : base() {
      // Target required Views (use the TargetXXX properties) and create their Actions.
      TargetObjectType = typeof(TStudentBo);
    }

    private void ObjectSpace_CustomDeleteObjects(object sender, CustomDeleteObjectsEventArgs e) {
      foreach (object bDeletingObject in e.Objects) {
        var bItem = ObjectSpace.GetObject(bDeletingObject) as TStudentBo;
        bItem.Name += bItem.Age;
      }
      // 这行代码执行后，默认的删除操作就不再执行了
      e.Handled = true;
    }

    protected override void OnActivated() {
      base.OnActivated();
      // Perform various tasks depending on the target View.
      ObjectSpace.CustomDeleteObjects += ObjectSpace_CustomDeleteObjects;

      View.CollectionSource.Criteria["自定义搜索"] = CriteriaOperator.FromLambda<TStudentBo>(s => false == s.Name.EndsWith(s.Age.ToString()));
    }

    protected override void OnDeactivated() {
      // Unsubscribe from previously subscribed events and release other references and resources.
      ObjectSpace.CustomDeleteObjects -= ObjectSpace_CustomDeleteObjects;
      base.OnDeactivated();
    }
    protected override void OnViewControlsCreated() {
      base.OnViewControlsCreated();
      // Access and customize the target View control.
    }
  }
}
